package com.study.algo;

public class BinarySearchDemo
{
    public static void main(String[] args)
    {
        int[] arr = {7, 23, 79, 81, 103, 127, 131, 147};
        System.out.println(binarySearch(arr, 81));
    }

    public static int binarySearch(int[] arr, int target)
    {
        int left = 0, right = arr.length - 1;
        while (true) {
            if (left > right) {
                return -1;
            }
            int mid = (left + right) / 2;
            if (arr[mid] > target) {
                right = mid - 1;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
    }
}
